from rest_framework import serializers
from .models import NavCategory, Banner, CourseCategory

class NavCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = NavCategory
        fields = ['id', 'name', 'display_name', 'weight', 'is_active', 
                 'theme_color', 'show_countdown', 'external_link']

class BannerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Banner
        fields = ['id', 'title', 'link', 'image', 'note', 'weight']

class CourseCategorySerializer(serializers.ModelSerializer):
    """课程分类序列化器"""
    class Meta:
        model = CourseCategory
        fields = ['id', 'category_name', 'parent', 'level', 'description', 'created_at', 'updated_at']

class CourseCategoryListSerializer(serializers.ModelSerializer):
    """课程分类列表序列化器"""
    children = serializers.SerializerMethodField()

    class Meta:
        model = CourseCategory
        fields = ['id', 'category_name', 'level', 'description', 'children']

    def get_children(self, obj):
        """获取子分类"""
        children = CourseCategory.objects.filter(parent=obj)
        if children:
            return CourseCategoryListSerializer(children, many=True).data
        return [] 